package tcu.iot.gasmanage.common.utils;

import java.text.DateFormat;
import java.text.DateFormat.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class DateUtils {
    //前端传入的时间中包含T，Z字母，将其过滤掉
    public static String dealDateFormat(String OldDate) {
        Date date1 = null;
        DateFormat df2 = null;
        try {
            OldDate = OldDate.replace("Z", " UTC");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
            Date date = df.parse(OldDate);
            SimpleDateFormat df1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
            date1 = df1.parse(date.toString());
            df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return df2.format(date1);
    }

    public static String SwitchTimestamp(String OldDate) {
        StringBuffer date = new StringBuffer(OldDate);
        date.setCharAt(10, 'T');
        String newDate = date.toString();
        return newDate + "Z";
    }

    public static String ReduceAccuracy(String OldDate) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = df.parse(OldDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return df.format(date);
    }

    public static String ReduceAccuracyDate(String OldDate) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = df.parse(OldDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return df.format(date);
    }

    //规避前端bug，将前端传入的时间String中的日期+1
    public static String SetTimeAdd1(String OldDate) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Calendar calendar = Calendar.getInstance();
            Date NewDate = df.parse(OldDate);
            calendar.setTime(NewDate);
            calendar.add(Calendar.DAY_OF_MONTH, 1);//加一天
            String date = df.format(calendar.getTime());
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String SetTimeReduce1(String OldDate) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Calendar calendar = Calendar.getInstance();
            Date NewDate = df.parse(OldDate);
            calendar.setTime(NewDate);
            calendar.add(Calendar.DAY_OF_MONTH, -1);//加一天
            String date = df.format(calendar.getTime());
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String localToUTC(String localTimeStr) {
        try {
            Date localDate = getLocalSDF().parse(localTimeStr);
            return getUTCSDF().format(localDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static String utcToLocal(String utcTimeStr) {
        try {
            Date date = getUTCSDF().parse(utcTimeStr);
            return getLocalSDF().format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static SimpleDateFormat getLocalSDF() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:sss");
    }

    private static SimpleDateFormat getUTCSDF() {
        SimpleDateFormat utcSDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        utcSDF.setTimeZone(TimeZone.getTimeZone("UTC"));
        return utcSDF;
    }

}
